using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FrameWorkSong;


public class AxisControl : MonoBehaviourSimplify
{
    public enum RotationFront
    {
        X, Y,Z
    }
    public List<CubeControl> Cubes;
    public RotationFront rotationFront;
     Collider _collider;
    RubikManager rubikManager;
    Vector3 vector = Vector3.zero;
    public RubikManager RubikManager
    {
        get
        {
            if (rubikManager==null)
            {
                rubikManager = RubikManager.Instance;
            }
            return rubikManager;
        }
    }

    public Collider Collider
    {
        get
        {
            if (_collider==null)
            {
                _collider = gameObject.GetComponent<Collider>();
            }
            return _collider;
        } }


    // Start is called before the first frame update
    void Start()
    {
        Delay(1, () => { FindSelfCube(); });
        vector = transform.eulerAngles;
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void FindSelfCube()
    {
        Cubes = new List<CubeControl>() ;
        List<CubeControl> allCubeControls = RubikManager.CubeControls;
        int length = allCubeControls.Count;
        for (int i = 0; i < length; i++)
        {
            if (Collider.bounds.Intersects(allCubeControls[i].Collider.bounds))
            {
                Cubes.Add(allCubeControls[i]);
                allCubeControls[i].AxisControls.Add(this);
            }
        }
    }

    public override void OnBeforeDestroy()
    {
        
    }
    public void ToRotate(int front)
    {
        int length = Cubes.Count;
        for (int i = 0; i < length; i++)
        {
            Cubes[i].SetParent(transform);
        }
       
        if (rotationFront== RotationFront.X)
        {
             vector += (front* new Vector3(90, 0, 0));
            if (vector.x>=360)
            {
                vector = Vector3.zero;
            }
         
           
        }
        if (rotationFront == RotationFront.Y)
        {
            vector += (front * new Vector3(0, 90, 0));
            if (vector.y>= 360)
            {
                vector = Vector3.zero;
            }

        }
        if (rotationFront == RotationFront.Z)
        {
            vector += (front * new Vector3(0, 0, 90));
            if (vector.z >= 360)
            {
                vector = Vector3.zero;
            }
        }
        DoRotate(transform, vector, 0.5f,()=> { RubikManager.Reset(); });
    }
}
